import 'package:flutter/material.dart';

void main() => runApp(HomePage());

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

// 注意：SingleTickerProviderStateMixin为单一动画，组合动画用TickerProviderStateMixin
class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
  Animation<double> _doubleAnimation;
  AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(vsync: this, duration: const Duration(seconds: 2))..forward();
    // 注意：这里必须指定泛型，否则报错
    _doubleAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(_animationController)
      ..addListener(() {
        print(_doubleAnimation.value.toString());
        setState(() {});
      })
      ..addStatusListener((status) {
        print('status == $status');
        if (status == AnimationStatus.completed) {
          _animationController.reverse();
        }
        if (status == AnimationStatus.dismissed) {
          _animationController.forward();
        }
      });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("animated widget"),
        ),
        body: Center(
          child: Center(
            child: MyLogo(
              animation: _doubleAnimation,
            ),
          ),
        ),
      ),
    );
  }
}

class MyLogo extends AnimatedWidget {
  MyLogo({Key key, @required Animation animation}) : super(key: key, listenable: animation);

  final Tween<double> _rotateAnim = Tween(begin: 20.0, end: 45.0);
  final Tween<double> _scaleAnim = Tween(begin: 1, end: 2);

  @override
  Widget build(BuildContext context) {
    final Animation<double> anim = listenable;
    return Transform.scale(
      scale: _scaleAnim.evaluate(anim),
      child: Transform.rotate(
        angle: _rotateAnim.evaluate(anim),
        child: Container(
          width: 200,
          height: 200,
          color: Colors.red,
          child: FlutterLogo(),
        ),
      ),
    );
  }
}
